package org.example.mulitithreading.task_denial_strategy.AbortPolicy;

import java.util.concurrent.*;

public class TestAbortPolicy {

    private static final int THREADS_SIZE = 1;
    private static final int THREADS_MAX_SIZE = 3;
    private static final int TASK_MAX = 10;
    private static final int CAPACITY = 1;

    public static void main(String[] args) throws Exception {

        // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(THREADS_SIZE)，"线程池"的阻塞队列容量为1(CAPACITY)。
        ThreadPoolExecutor pool = new ThreadPoolExecutor(THREADS_SIZE, THREADS_MAX_SIZE, 0, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(CAPACITY), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        // 新建10个任务，并将它们添加到线程池中。
        for (int i = 0; i < TASK_MAX; i++) {
            int finalI = i;
//            try {
                pool.execute(() -> System.out.println(Thread.currentThread().getName() + "---->> 执行了任务" + finalI));
//            } catch (RejectedExecutionException e) {
//                System.out.println(e);
//            }
        }

        // 关闭线程池
        pool.shutdown();

    }
}
